package com.sxhuayuan.parking.service.impl;

import com.sxhuayuan.parking.compenent.Filter;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.dao.*;
import com.sxhuayuan.parking.entity.Order;
import com.sxhuayuan.parking.entity.ParkingIn;
import com.sxhuayuan.parking.entity.ParkingInOut;
import com.sxhuayuan.parking.entity.ParkingLot;
import com.sxhuayuan.parking.service.ParkChannelService;
import com.sxhuayuan.parking.service.ParkingInOutService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Log4j2
@Service("parkingInOutServiceImpl")
public class ParkingInOutServiceImpl extends BaseServiceImpl<ParkingInOut, Long> implements ParkingInOutService {

	@Autowired
	ParkingInOutDao dao;
	@Autowired
	ParkingInDao inDao;
	@Autowired
	ParkingLotDao lotDao;
	@Autowired
	ParkChannelService exitService;
	@Autowired
	OrderDao orderDao;

	@Override
	public BaseDao<ParkingInOut, Long> getDAO() {
		return dao;
	}

	@Override
	public ParkingInOut out(ParkingIn in, Date outTime, Long exitId) {
		return out(in, outTime, exitId, null);
	}

	@Override
	public ParkingInOut out(ParkingIn in, Date outTime, Long exitId, String lotRecordId) {
		ParkingInOut out = new ParkingInOut();
		out.setCardNo(in.getCardNo());
		out.setCarPlate(in.getCarPlate());
		out.setEntranceId(in.getEntranceId());
		out.setInTime(in.getInTime());
		out.setParkingLotId(in.getParkingLotId());
		out.setUniqueId(in.getUniqueId());
		out.setExitId(exitId);
		out.setOutTime(outTime);
		out.setLotRecordId(lotRecordId);
		dao.save(out);
		inDao.delete(in);
		return out;
	}

	@Override
	public ParkingInOut out(ParkingIn in, ParkingInOut out) {
		out.setCardNo(in.getCardNo());
		out.setCarPlate(in.getCarPlate());
		out.setEntranceId(in.getEntranceId());
		out.setInTime(in.getInTime());
		out.setParkingLotId(in.getParkingLotId());
		out.setUniqueId(in.getUniqueId());
		dao.save(out);
		inDao.delete(in);
		return out;
	}

	@Override
	public ParkingInOut findByUniqueId(Long uniqueId) {
		return dao.findByUniqueId(uniqueId);
	}

	@Override
	public ParkingInOut findByParkingLotIdAndLotRecordId(Long lotId, String recordId) {
		return dao.findByParkingLotIdAndLotRecordId(lotId, recordId);
	}

	@Override
	public int countByParkingLotIdAndLotRecordId(Long lotId, String recordId) {
		return dao.countByParkingLotIdAndRecordId(lotId, recordId);
	}

	@Override
	public ParkingInOut findByInTimeAndParkingLotIdAndCarPlate(Date inTime, Long parkId, String carPlate) {
		return dao.findByInTimeAndParkingLotIdAndCarPlate(inTime, parkId, carPlate);
	}

	@Override
	public ParkingInOut outMerge(ParkingIn in, Date outTime, Order order) {
		Date inTime = order.getEntertime();
		String lotSn = order.getParkingLotSn();
		ParkingLot lot = lotDao.findBySn(lotSn);
		String carPlate = order.getCarPlate();
		ParkingInOut inOut = dao.findByInTimeAndParkingLotIdAndCarPlate(inTime, lot.getId(), carPlate);
		if (inOut == null) {
			return out(in, outTime, null);
		}
		Date lastOutTime = inOut.getOutTime();
		log.info("合并入场记录:[{}]->[{}]", in.getUniqueId(), inOut.getUniqueId());
		inOut.setOutTime(order.getEndtime());
		order.setUniqueId(inOut.getUniqueId());
		order.setStarttime(lastOutTime);
		orderDao.save(order);
		inDao.delete(in);
		return dao.save(inOut);
	}

	@Override
	public ParkingInOut findByParkingLotIdAndCarPlateOne(Long lotId, String carPlate) {
		return dao.findByParkingLotIdAndCarPlateOne(lotId, carPlate);
	}

	@Override
	public Page<ParkingInOut> findByPage(Pageable pageable) {
		Assert.notNull(pageable, "pageable不能为空");
		StringBuilder sb = new StringBuilder();
		List<Filter> filters = pageable.getFilters();
		boolean isParkingName = false;
		if (filters != null) {
			for (Filter filter : filters) {
				if ("parkingLotName".equals(filter.getProperty())) {
					isParkingName = true;
				}
				setCondition(sb, filter);
			}
		}
		if (isParkingName) {
			sb.insert(0, "select out from ParkingInOut out left join ParkingLot p on out.parkingLotId=p.id where 1=1 ");
		} else {
			sb.insert(0, "select out from ParkingInOut out where 1=1 ");
		}
		sb.append(" order by out.outTime desc");
		return findPage(sb.toString(), sb.toString().replace("select out", "select count(out.id)"), pageable.getPageSize(), pageable.getPageNumber());
	}

	/** 组装查询条件 */
	private void setCondition(StringBuilder sb, Filter filter) {
		if (filter.getProperty() == null || "".equals(filter.getProperty()) || filter.getValue() == null || filter.getValue().equals("")) {
			return;
		}
		switch (filter.getProperty()) {
		case "istartTime": {// 出场开始时间
			sb.append(" and out.outTime>='").append(filter.getValue()).append("' ");
			break;
		}
		case "iendTime": {// 出场结束时间
			sb.append(" and out.outTime<='").append(filter.getValue()).append("' ");
			break;
		}
		case "parkingLotName": {// 车场名称
			sb.append(" and p.name='").append(filter.getValue()).append("' ");
			break;
		}
		case "carPlate": {// 车牌号
			sb.append(" and out.carPlate='").append(filter.getValue()).append("' ");
			break;
		}
		case "exitId": {// 出口id
			sb.append(" and out.exitId='").append(filter.getValue()).append("' ");
			break;
		}
		case "uniqueId": {// 唯一编号
			sb.append(" and out.uniqueId='").append(filter.getValue()).append("' ");
			break;
		}
		case "carActType": {// 车辆操作类型 0临时 1包月 2免费 3错误
			sb.append(" and out.carActType='").append(filter.getValue()).append("' ");
			break;
		}
		default:
		}
	}

}
